/**
 * 
 */
package com.kunbo.cn.service.impl.core.handset;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.dao.core.handset.HandsetUsersDao;
import com.kunbo.cn.entity.core.handset.HandsetUsers;
import com.kunbo.cn.service.core.handset.HandsetUsersService;
import com.kunbo.cn.util.date.SDate;
import com.kunbo.cn.util.encrypt.MD5Utils;

/**
 *
 * @author bjl  
 * @date 2019年3月17日 
 */
@Service
public class HandsetUsersServiceImpl implements HandsetUsersService {
	@Autowired
    private  JdbcTemplate jdbc;
	@Autowired
	private HandsetUsersDao hanuDao;
	
	@Override
	public void save(HandsetUsers handuser) {
		handuser.setCreate_time(SDate.getSystemDateToString());
		handuser.setPassword(MD5Utils.md5(handuser.getPassword()));
		hanuDao.save(handuser);
	}
	
	@Override
	public void update(HandsetUsers handuser) {
		handuser.setLast_update_time(SDate.getSystemDateToString());
		hanuDao.save(handuser);
	}
	
	@Override
	public void delete(String id) {
		hanuDao.delete(id);
	}
	
	@Override
	public HandsetUsers findById(String id) {
		return hanuDao.findOne(id);
	}
	@Override
	public Page<HandsetUsers> findHandsetUsers(String handuser_code,String name,String account,
			Integer pageNo, Integer pageSize) {
		Pageable pageable = new PageRequest(pageNo-1, pageSize);
        Specification<HandsetUsers> confusion=new Specification<HandsetUsers>() {
            @Override
            public Predicate toPredicate(Root<HandsetUsers> root, CriteriaQuery<?> query,
                    CriteriaBuilder criteriaBuilder) {
            	List<Predicate> predicates = new ArrayList<>();
            	if(!"".equals(handuser_code)) {
            		predicates.add(criteriaBuilder.like(root.get("handuser_code"), "%"+handuser_code+"%"));
            	}
            	if(!"".equals(name)) {
            		predicates.add(criteriaBuilder.like(root.get("name"), "%"+name+"%"));
            	}
            	if(!"".equals(account)) {
            		predicates.add(criteriaBuilder.like(root.get("account"), "%"+account+"%"));
            	}
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return hanuDao.findAll(confusion, pageable);
	}

	@Override
	public HandsetUsers findCurrentAccount(String account, String password) {
		// TODO Auto-generated method stub
		return hanuDao.findByAccountAndPassword(account, password);
	}

	@Override
	public HandsetUsers findByName(String name) {
		// TODO Auto-generated method stub
		return hanuDao.findByName(name);
	}
}
